package com.app.teaware.stock;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.BasisBean;
import com.app.teaware.bean.BillListData;
import com.app.teaware.bean.LoginBean;
import com.app.teaware.bean.StockListBean;
import com.app.teaware.bean.StockListData;
import com.app.teaware.dialog.CheckKWDialog;
import com.app.teaware.goods.AddOrEditGoodsInfoActivity;
import com.app.teaware.util.MyUtils;
import com.app.teaware.util.UHFUtil;

import java.util.Calendar;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.MathUtils;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

public class CheckWareActivity extends BasisActivity {


    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.edit_gno)
    EditText edit_gno;
    StockListBean listBean;

    MyAdapterList mAdapter;

    Calendar calendar;


    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.check_ware_activity);
        setTitle("库存查询");
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if(!Constants.ACTION_UHF_RESULT_SEND.equals(action))
                return ;
            Parcelable[] tagInfos =  intent.getParcelableArrayExtra(Constants.EXTRA_TAG_INFO);
            edit_gno.setText(UHFUtil.getEPCId(tagInfos).trim().toUpperCase(Locale.ROOT));
            getList();
        }//end onReceiver
    };

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        listBean = new StockListBean();
        calendar = Calendar.getInstance();
//        initSpiner();
    }

    private boolean httpIsFinish = true;
    private void getList(){
        if(TextUtils.isEmpty(edit_gno.getText().toString())){
            showToast("请先获取库位码");
            return;
        }
        if(!httpIsFinish) return;
        httpIsFinish = false;
        showLoading();
        MyParams params = new MyParams();
        params.put("aid",LoginBean.getInstance().getAcode());
        params.put("rfid",edit_gno.getText().toString());


        MyHttpUtils.get(Constants.URL_STOCK_SELECT_WMS_LIST, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        listBean = (StockListBean) result;
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        dismissProgress();
                        listBean.getList().clear();
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        httpIsFinish = true;
                        dismissProgress();
                    }
                },
                0, StockListBean.class);
    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {
        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.base_list_item_and_right_arrow, viewGroup, false);
            return new ViewHolder(view);
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            int count = listBean == null ? 0 : listBean.getList().size();
            return count;
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.list_item)
            public LinearLayout list_item;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                StockListData data = listBean.getList().get(position);
                MyUtils.setNotNullText(text_name, data.getWms());
                list_item.setTag(data);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                StockListData data = (StockListData) v.getTag();
                Bundle extras = new Bundle();
                extras.putSerializable("wms", data.getWms());
                ActivityTool.skipActivity(mContext, StockInfoActivity.class,extras);

            }
        }


    }


    @Override
    protected void onPause() {
        super.onPause();
        unRegisterResultReceiver();
    }


    @Override
    protected void onResume() {
        super.onResume();
        registerResultReceiver();
    }

    private void registerResultReceiver() {
        try {
            IntentFilter iFilter = new IntentFilter(Constants.ACTION_UHF_RESULT_SEND);
            mContext.registerReceiver(mResultReceiver, iFilter);
        } catch (Exception e) {
        }

    }

    private void unRegisterResultReceiver() {
        try {
            mContext.unregisterReceiver(mResultReceiver);
        } catch (Exception e) {
        }

    }

}
